package com.xcf.controller.business.common;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.boxtypeManage.BoxTypeManageService;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/boxTypeManage")
public class BoxTypeManageController extends BaseController{
	@Resource
	private BoxTypeManageService boxTypeManageService; 
	/**
	 * 查找箱型管理信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView findBoxTypeManageInfo()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/common/boxtypeManage/box_type_manage_list");
		return view;
		
	}
	/**
	 * 查找信息并传入前端
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/value")
	@ResponseBody
	public Object entityMaintainListInfo(int page, int limit)throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		PageHelper.startPage(page, limit);	
		List<PageData> list = boxTypeManageService.findBoxTypeManageInfo(pd);
		List<PageData> list1 = new ArrayList<>();
		for(PageData p : list){
			 p.put("ID", p.get("ID"));
			 p.put("FAST_SEARCH_CODE", p.getString("FAST_SEARCH_CODE"));
			 p.put("BOX_TYPE_ABBREVIATION", p.getString("BOX_TYPE_ABBREVIATION"));
			 int i=Integer.parseInt(p.get("ENABLE").toString());
			 if(i==1){
				 p.put("ENABLE","是");
			 }else{
				 p.put("ENABLE","否");
			 }
			 p.put("BOX_TYPE_NAME", p.getString("BOX_TYPE_NAME"));
			 p.put("TYPE_CODE", p.getString("TYPE_CODE"));
			 p.put("EMPTY_BOX_WEIGHT", p.get("EMPTY_BOX_WEIGHT"));
			 p.put("SORT_CODE", p.get("SORT_CODE"));
	            list1.add(p);
		}
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 保存或者更新信息
	 * @return
	 * @throws Exception
	 */
	 @RequestMapping(value="/saveOrUpdate")
	 @ResponseBody
	public String saveOrUpdate()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("ID").isEmpty()){
			boxTypeManageService.saveBoxTypeManageInfo(pd);
		}else{
			boxTypeManageService.updateBoxTypeManageInfo(pd);
		}
		return null;
		
	}
	 /**
	  * 删除信息
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value="/deleteInfo")
	 @ResponseBody
	 public String deleteInfo()throws Exception{
			PageData pd = new PageData();
			pd = this.getPageData();

			String data = pd.getString("array");

			JSONArray items = JSONArray.fromObject(data);
			for(int i=0,size=items.size();i<size;i++){
				JSONObject item = items.getJSONObject(i);
				String id = item.getString("id").trim();
				PageData pdq = new PageData();
				pdq.put("ID", id);
				boxTypeManageService.deleteBoxTypeManageInfo(pdq);
			}
		return null;
		 
	 }
}
